package com.tgpms.web.earlierStage.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.ProjectInfo;
import com.tgpms.web.earlierStage.service.EarlierProjectInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "EarlierProjectInfoController", tags = "前期 项目信息相关接口")
@RestController
@RequestMapping(value = "/ev/earlierProjectInfo")
public class EarlierProjectInfoController {
	
	@Autowired
	private EarlierProjectInfoService earlierProjectInfoService;
	@Autowired
	private TokenService tokenService;

	@ApiOperation(value = "新增项目信息接口", notes = "新增项目信息接口", httpMethod = "POST")
	@PostMapping(value = "/addProjectInfo")
	public Result addProjectInfo(@RequestBody ProjectInfo projectInfo,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(projectInfo.getObsProjectName()) || Common.isEmpty(projectInfo.getObsCode())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|编码为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			projectInfo.setInsertUser(insertName);
			result = earlierProjectInfoService.addProjectInfo(projectInfo, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "修改项目信息接口", notes = "修改项目信息接口", httpMethod = "PUT")
	@PutMapping(value = "/updateProjectInfo")
	public Result updateProjectInfo(@RequestBody ProjectInfo projectInfo,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(projectInfo.getPiId())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			projectInfo.setModifyUser(insertName);
			result = earlierProjectInfoService.updateProjectInfo(projectInfo, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		} 
		return result;
	}
	 
	@ApiOperation(value = "删除项目信息接口", notes = "删除项目信息接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteProjectInfo")
	public Result deleteProjectInfo(@RequestParam(value = "piId", required = true) String piId,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(piId)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			result = earlierProjectInfoService.deleteProjectInfo(piId, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "查询项目信息树形接口", notes = "查询项目信息树形接口", httpMethod = "POST")
	@PostMapping(value = "/selProjectInfoForTree")
	public Result selProjectInfoForTree(HttpServletRequest request,
										@RequestParam(value = "usersId",required = false) String usersId) {
		Result result = new Result();
		try {
			List<Map<String, Object>> list = earlierProjectInfoService.selProjectInfoForTree(usersId, result);
			result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@ApiOperation(value = "无权限查询项目信息树形接口", notes = "无权限查询项目信息树形接口", httpMethod = "POST")
	@PostMapping(value = "/selAllProjectInfoForTree")
	public Result selProjectInfoForTree(HttpServletRequest request) {
		Result result = new Result();
		try {
			List<Map<String, Object>> list = earlierProjectInfoService.selAllProjectInfoForTree(result);
			result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
}
